﻿
namespace Common.Algorithm.Core.LocalTest;

public class FireflyExecute
{
    /// <summary>
    /// //萤火虫用于寻找最大 因此处理一下这个函数
    /// </summary>
    /// <param name="ranges"></param>
    /// <returns></returns>
    static Func<double[], double> GenFunc(out List<double[]> ranges)
    {
        HimmelblauFunction function = new HimmelblauFunction();
        ranges = function.ranges.ToList();
        Func<double[], double> func = (array) =>
        { 
            return -1 * function.GenMathFunction().Invoke(array);
        };
        return func;
    }

    /// <summary>
    /// 
    /// </summary>
    public static List<double[]> Run()
    {
        List<double[]> ranges = new List<double[]>();
        var func = GenFunc(out ranges);// function.GenMathFunction();

        FireflyService service = new FireflyService(func, ranges.ToList());

        Action action = () =>
        {
            service.RunAlgm(300);
        };
        Console.WriteLine($"time:{TimeService.TimeWatch(action)}");

        var bestLoc = FireflyService.fireflys.OrderBy(f => f.intensity).Last().position;
        Console.WriteLine($"{bestLoc[0]},{bestLoc[1]}");

        return FireflyService.records;
    }

    public static void RunMany()
    {
        for (int i = 0; i < 15; i++)
        {
            Run();
        }
    }
}
